package com.wjs.controller;

import com.github.pagehelper.PageInfo;
import com.wjs.entity.Category;
import com.wjs.entity.Tea;
import com.wjs.service.CategoryService;
import com.wjs.service.TeaService;
import com.wjs.service.impl.CategoryServiceImpl;
import com.wjs.service.impl.TeaServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/tea")
public class TeaController {
    private TeaService service=new TeaServiceImpl();
    private CategoryService service1=new CategoryServiceImpl();
    @RequestMapping("/selectAll")
    public String selectAll(Model model,Integer curPage){
        //设置默认页数，第一次的时候不会传过来数据所以是null就默认为第一页

        if(curPage==null){
            curPage=1;
        }

        //根据自己需要设置，一页几条数据
        int pageSize=5;

        PageInfo<Tea> pi=service.selectAllByPage(curPage,pageSize);
        model.addAttribute("pi",pi);
        return "/tealist.jsp";
    }


    @RequestMapping("/addTea")
    public String addTea(Tea tea){
        Date date=new Date();//此时date为当前的时间
        SimpleDateFormat dateFormat=new SimpleDateFormat("YYYY-MM-dd");//设置当前时间的格式，为年-月-日
        tea.setTime(dateFormat.format(date));

        service.addTea(tea);

        return "redirect:/tea/selectAll.do";
    }
    @RequestMapping("/selectById")
    public String selectById(int id,HttpSession session){
        Tea tea=service.selectById(id);
        session.setAttribute("Tea",tea);
        List<Category> list=service1.selectAll();
        session.setAttribute("categoryListTwo",list);
        return "redirect:/updateTea.jsp";
    }
    @RequestMapping("/update")
    public String update(Tea tea){
        service.update(tea);

        return "redirect:/tea/selectAll.do";
    }

    @RequestMapping("/deleteByID")
    public String deleteByID(int id){
        service.deleteByID(id);

        return "redirect:/tea/selectAll.do";
    }

    @RequestMapping("/batchDle")
    public String batchDle(int [] ids){
        service.batchDle(ids);
        return "redirect:/tea/selectAll.do";
    }

}
